﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace eGrid_App
{
    /// <summary>
    /// Interaction logic for CornerMenu.xaml
    /// </summary>
    public partial class CornerMenu : UserControl
    {
        /// <summary>
        /// location of the corner menu
        /// </summary>
        public enum CornerMenuLocation
        {
            TopLeft = 1,
            TopRight = 2,
            BottomLeft = 3,
            BottomRight = 4,
        }

        #region members
        
        private My_Application parentApp;

        private DispatcherTimer timer;
        private int ShrinkTime;

        private CornerMenuLocation location;
        public CornerMenuLocation Location
        {
            get
            {
                return location;
            }

            set
            {
                location = value;
            }
        }

        #endregion members

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="parentUIElement"></param>
        /// <param name="menuLocation"></param>
        public CornerMenu(My_Application parentUIElement, CornerMenuLocation menuLocation)
        {
            InitializeComponent();

            parentApp = parentUIElement;
            location = menuLocation;

            InitializeTimer();
        }

        
        #region event handlers

        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            timer.Stop();
            parentApp.showExitMenu(0.0 /*FIX ME: orientation*/, location);
        }

        private void ListButton_Click(object sender, RoutedEventArgs e)
        {
            parentApp.showListBox(0.0 /*FIX ME: orientation*/, location);
        }

        private void ConfigButton_Click(object sender, RoutedEventArgs e)
        {
            parentApp.ShowConfigMenu(location);
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            parentApp.viewSearchDialog();
        }

        private void CollapsedMenu_Click(object sender, RoutedEventArgs e)
        {
            ToggleMenuState();
        }

        private void ExpandedMenuEllipse_Click(object sender, RoutedEventArgs e)
        {
            ToggleMenuState();
        }

        private void ToggleMenuState()
        {
            if (ExpandedMenu.Visibility == Visibility.Hidden)
            {
                // Expanded menu is hidden
                timer.Start();

                CollapsedMenu.Visibility = Visibility.Hidden;
                ExpandedMenu.Visibility = Visibility.Visible;
                                
                Grid.SetZIndex(ExpandedMenu, 2);
                Grid.SetZIndex(CollapsedMenu, 1);
            }
            else
            {
                // Expanded menu is visible

                if (!parentApp.isExitDialogVisible())
                {
                    ExpandedMenu.Visibility = Visibility.Hidden;
                    CollapsedMenu.Visibility = Visibility.Visible;

                    Grid.SetZIndex(ExpandedMenu, 1);
                    Grid.SetZIndex(CollapsedMenu, 2);

                    timer.Stop();
                }
            }
        }
        
        #endregion

        #region animations

        /// <summary>
        /// 
        /// </summary>
        public void InitializeTimer()
        {
            this.ShrinkTime = 5;
            timer = new System.Windows.Threading.DispatcherTimer();
            timer.Tick += new EventHandler(dTimer_Tick);
            timer.Interval = new TimeSpan(0, 0, ShrinkTime);
        }

        public void startTimer()
        {
            timer.Start();
        }

        private void dTimer_Tick(object sender, EventArgs args)
        {
            if (((DispatcherTimer)sender == timer))
            {
                ExpandedMenu.Visibility = Visibility.Hidden;
                CollapsedMenu.Visibility = Visibility.Visible;
            }
        }
        #endregion

    }
}
